﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Threading;
using System.Windows.Forms;

using DZTC.UselessAppEver.Core;

namespace DZTC.UselessAppEver.UI
{
    public partial class ConversionForm : Form
    {
        private ConversionController Controller { get; set; }

        public ConversionForm(ConversionController controller)
        {
            InitializeComponent();

            ApplyLocalization();

            Controller = controller;

            Text = Localization.Converting;
            btCancel.Text = Localization.Cancel;

            // btCancel.Click += (sender, args) => Controller.Cancel();
        }

        private void ApplyLocalization()
        {
            btCancel.Text = Localization.Cancel;
        }

        public void UpdateProgress(int value)
        {
            pbConversionProgress.Value = value;
        }

        public void SetFileNames(string inputFile, string outputFile)
        {
            var text = Localization.ConvertingFileTo.AsFormat(inputFile, outputFile);
            if (text.Length > 80) text = text.Substring(0, 80) + "..."; 

            gbProgress.Text = text;
        }


        private void CancelClick(object sender, EventArgs e)
        {
            Controller.Cancel(false);
        }

        // I love Winforms SO MUCH

        private bool CloseCausedNotByUser { get; set; }

        public void CloseForm()
        {
            CloseCausedNotByUser = true;
            Close();
            CloseCausedNotByUser = false;
        }

        private void OnFormClose(object sender, FormClosingEventArgs e)
        {
            if (CloseCausedNotByUser) return;

            e.Cancel = true;
            Controller.Cancel(true);
        }

        private void FormShown(object sender, EventArgs e)
        {
            // probably, not the best solution
            Controller.Start();
        }
    }
}